<?php

namespace app\home\model;
use think\Model;
use think\Db;

class CommentModel extends Model
{
    protected $name = 'comment'; // 设置数据表
    protected $autoWriteTimestamp = true;
    /**
     * insertOneComment [ 插入一条评论 ]
     * @author [Boot.Z] [852952656@qq.com]
     */
    public function insertOneComment($param)
    {

        $data['content'] = $param['content'];
        $data['nickname'] = $param['user'];
        $data['u_id'] = $param['u_id'];
        $data['article_id'] = $param['article_id'];


        if ($this->allowField(true)->save($data)){
            return ['code' => 1,'msg'=>'评论成功'];
        }else{
            return ['code' => 0,'msg'=>'评论失败'];
        }

    }

    /**
     * fetchAllComment [ 查出所有以及评论 ]
     * @author [Boot.Z] [852952656@qq.com]
     */
    public function fetchAllComment($id)
    {
        return $this->where(array('p_id' => 0,'article_id' => $id))
                    ->paginate('10');
    }

    public function getCommentCount($id)
    {
       return $this -> where(array('article_id'=> $id)) ->count();

    }
}